//
//  MYGChatVoiceCell.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/15.
//

import UIKit
import SVGKit

class MYGChatVoiceCell: UITableViewCell {
    // MARK: - Property
    var model: MYGChatVoiceCellModel? {
        didSet {
            guard let model = model else {
                return
            }
            
            reloadLayout()
            
            if model.isLeft {
                animateView.layer.transform = CATransform3DMakeRotation(0, 0, 0, 1)

            } else {
                animateView.layer.transform = CATransform3DMakeRotation(CGFloat.pi, 0, 0, 1)
            }
            
            if model.isLeft == false {
                self.avatar.sd_setImage(with: URL(string: MYGUserManager.user?.doctor.avatar), placeholderImage: UIImage(named: "resum_head"))
            }else {
                if model.avatar.contains("http") {
                    avatar.sd_setImage(with: URL(string: model.avatar))
                } else {
                    avatar.image = UIImage(named: "head")
                }
            }
            
//            if model.avatar.contains("http") {
//                avatar.sd_setImage(with: URL(string: model.avatar))
//            } else {
//                if let data = UserDefaults.standard.data(forKey: model.avatar) {
//                    guard let svgImage = SVGKImage(data: data) else {
//                        return
//                    }
//                    
//                    self.avatar.image = svgImage.uiImage
//                } else {
//                    DispatchQueue.global().async {
//                        let url = URL(string: model.avatar)!
//                        guard let data = try? Data(contentsOf: url) else {
//                            return
//                        }
//                        
//                        UserDefaults.standard.setValue(data, forKey: model.avatar)
//                        UserDefaults.standard.synchronize()
//                        guard let svgImage = SVGKImage(data: data) else {
//                            return
//                        }
//                
//                        DispatchQueue.main.async {
//                            self.avatar.image = svgImage.uiImage
//                        }
//                    }
//                }
//            }
//            if !model.isLeft {
//                self.avatar.sd_setImage(with: URL(string: MYGUserManager.user?.doctor.avatar), placeholderImage: UIImage(named: "resum_head"))
//            }
            nameLabel.text = model.name
            timeLabel.text = "\(model.duration)'"
            
        }
    }
    
    
    func timeChange(_ isOver: Bool, time: Int) {
        if isOver {
            self.animateView.stopAnimating()
            timeLabel.text = "\(model?.duration ?? 0)'"
        } else {
            if !self.animateView.isAnimating {
                self.animateView.startAnimating()
            }
        }
        
        guard let model = model else {
            return
        }
        timeLabel.text = "\(model.duration - time)'"
    }
    
    var playAction: (()->Void)?
    var withdrawAction: (()->Void)?
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(mainView)
        mainView.addSubview(animateView)
        contentView.addSubview(timeLabel)
        
        reloadLayout()
    }
    
    private func reloadLayout() {
        guard let isLeft = model?.isLeft else {
            return
        }
        
        avatar.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalToSuperview().offset(25)
            } else {
                make.right.equalToSuperview().inset(25)
            }
            make.top.equalToSuperview().offset(20)
            make.width.height.equalTo(36)
        }
        
        nameLabel.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalTo(avatar.snp.right).offset(10)
            } else {
                make.right.equalTo(avatar.snp.left).offset(-10)
            }
            make.top.equalTo(avatar.snp.top)
        }
        
        mainView.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalTo(avatar.snp.right).offset(10)
            } else {
                make.right.equalTo(avatar.snp.left).offset(-10)
            }
            make.top.equalTo(nameLabel.snp.bottom).offset(7)
            make.width.equalTo(112)
            make.height.equalTo(38)
            make.bottom.equalToSuperview().inset(5)
        }
        
        animateView.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalToSuperview().offset(13)
            } else {
                make.right.equalToSuperview().inset(13)
            }
            make.centerY.equalToSuperview()
            make.width.equalTo(12)
            make.height.equalTo(20)
        }
        
        timeLabel.snp.remakeConstraints { make in
            if isLeft {
                make.left.equalTo(animateView.snp.right).offset(10)
            } else {
                make.right.equalTo(animateView.snp.left).offset(-10)
            }
            make.centerY.equalTo(mainView)
        }
    }

    // MARK: - lazy load
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 18
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(16)
        label.textColor = .rgba(38, 38, 40, 1)
        return label
    }()
    
    private(set) lazy var mainView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(247, 247, 247, 1)
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 4
        let tap = UITapGestureRecognizer(target: self, action: #selector(playVoice))
        view.addGestureRecognizer(tap)
        view.isUserInteractionEnabled = true
        
        let r = UILongPressGestureRecognizer(target: self, action: #selector(showBack))
        view.isUserInteractionEnabled = true
        view.addGestureRecognizer(r)
        
        return view
    }()
    
    private lazy var animateView: UIImageView = {
//        let imgV = LottieAnimationView(name: "Lottie/voice")
        let imgs: [UIImage] = [UIImage(named: "Chat/voice3")!, UIImage(named: "Chat/voice1")!,UIImage(named: "Chat/voice2")!]
        let imgV = UIImageView(image: UIImage(named: "Chat/voice3"))
        imgV.contentMode = .left
        imgV.animationImages = imgs
        imgV.animationDuration = 1
        imgV.animationRepeatCount = 0
//        imgV.startAnimating()
        return imgV
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(51, 51, 51, 1)
        return label
    }()
}

extension MYGChatVoiceCell {
    @objc
    private func playVoice() {
        self.playAction?()
    }
    
    @objc
    private func showBack() {
        self.withdrawAction?()
    }
}
